﻿using FastExecutor.Base.Annotation;
using FastExecutor.Base.Util;
using FastExecutor.Message.Application;
using FastExecutor.ORG.DB;
using FastExecutor.ORG.Entity;
using FastExecutor.ORG.Enum;
using FastExecutor.ORG.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace FastExecutor.ORG.Filter
{
    public class LogFilter : ActionFilterAttribute
    {
        private AuthUtil @AuthUtil = InjectUtil.Container.Resolve<AuthUtil>();
        private DB_Module @DB_Module = InjectUtil.Container.Resolve<DB_Module>();
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var Request = filterContext.RequestContext.HttpContext.Request;
            var Response = filterContext.RequestContext.HttpContext.Response;
            bool IsController = filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(FastAuthAttribute), true);
            bool IsAnonymous = @AuthUtil.CheckIsAnonymousAccess(Request.Url.ToString());
            //页面路由
            if (!filterContext.HttpContext.Request.IsAjaxRequest())
            {
                Frame_VisitLog LogEntity = new Frame_VisitLog();
                LogEntity.RowGuid = CommonUtil.CreateCommonGuid();
                LogEntity.FromIP = CommonUtil.GetClientIP();
                LogEntity.VisitDateTime = DateTime.Now;
                LogEntity.VisitDate = DateTime.Now.ToString("yyyy-MM-dd");
                LogEntity.VisitYear = DateTime.Now.Year;
                LogEntity.VisitMonth = DateTime.Now.Month;
                LogEntity.VisitDay = DateTime.Now.Day;
                string ModuleUrl = filterContext.HttpContext.Request.Url.ToString().Replace(CommonUtil.GetRootPath(), string.Empty);
                Frame_Module ModuleEntity = @DB_Module.GetModuleWithUrl(ModuleUrl);
                if (ModuleEntity != null)
                {
                    LogEntity.ModuleGuid = ModuleEntity.ParentModuleGuid;
                    LogEntity.ModuleName = ModuleEntity.ModuleName;
                    LogEntity.ModuleUrl = ModuleEntity.ModuleUrl;
                    LogEntity.ModuleCode = ModuleEntity.ModuleCode;
                    if (IsController && IsAnonymous)
                    {
                        //匿名访问
                        LogEntity.UGuid = "Anonymous";
                        LogEntity.UName = "Anonymous";
                        LogEntity.DGuid = "Anonymous";
                        LogEntity.DName = "Anonymous";
                    }
                    else
                    {
                        //验证访问
                        string TokenStr = @AuthUtil.GetTokenString(Request);
                        TokenEntity Token = @AuthUtil.GetToken(TokenStr);
                        LogEntity.UGuid = Token.UserGuid;
                        LogEntity.UName = Token.DisplayName;
                        LogEntity.DGuid = Token.OUGuid;
                        LogEntity.DName = Token.OUName;

                    }
                    MessageCenter.GetInstance().SendMessage(FrameEnum.LogType.Visit.ToString(), LogEntity);
                }

            }

            base.OnActionExecuting(filterContext);
        }
    }
}
